<template>
	<view class="followup_page">
		<view class="list" v-for="(item, index) in caseList" :key="index">
			<view class="cate">
				分类{{++index}}
			</view>
			<view class="item" v-for="(value, index) in item" :key="index">
				<view class="item_left">
					{{value.name}}
				</view>
				<view class="item_right" @tap="handleClick(value.id)">
					<image v-if="value.id == selectId" src="../../static/radio_check1@2x.png" mode=""></image>
					<image v-else src="../../static/radio_check@2x.png" mode=""></image>
				</view>
			</view>
		</view>
		<view class="btn" @tap="handleConfirm">
			确认选择
		</view>
	</view>
</template>

<script>
	import {
		defineComponent,
		reactive,
		toRefs,
		getCurrentInstance
	} from "vue"
	import {
		onLoad
	} from '@dcloudio/uni-app'
	export default defineComponent({
		props: {
			id: String
		},
		setup(props) {
			const data = reactive({
				caseList: [],
				selectId: ''
			})

			const {
				appContext: {
					config: {
						globalProperties: global

					},
				},
			} = getCurrentInstance()
			
			// 数组分类
			const groupArr = (list, field) => {
				var fieldList = [],
					att = [];
				list.map((e) => {
					fieldList.push(e[field])
				})
				//数组去重
				fieldList = fieldList.filter((e, i, self) => {
					return self.indexOf(e) == i
				})
				for (var j = 0; j < fieldList.length; j++) {
					//过滤出匹配到的数据
					var arr = list.filter((e) => {
						return e[field] == fieldList[j];
					})
					att.push(arr)
				}
				return att;
			}

			onLoad(() => {
				// 获取病历随访方案列表
				global.$request.post('/v1/api/visit/list', {
					page: 1,
					pets_category_id: props?.id
				}).then(res => {
					if (res.code == 1) {
						// data.caseList = res.data
						data.caseList = groupArr(res.data, 'triage_category_id')
						console.log('病历随访方案列表', data.caseList)
					}
				})
			})

			const handleClick = (id) => {
				data.selectId = id
			}

			const handleConfirm = () => {
				const allList = data.caseList.flat()
				const selectObj = allList.find(item => item.id === data.selectId)
				if (selectObj == undefined) {
					uni.showToast({
						title: '请选择随访方案',
						icon: 'none',
						duration: 2000
					})
					return
				}
				let pages = getCurrentPages()
				if (pages.length >= 2) {
					let curPage = pages[pages.length - 1]; // 当前页面
					let prePage = pages[pages.length - 2]; // 上一页面
					prePage.setData({
						id: selectObj.id,
						name: selectObj.name
					})
					uni.navigateBack()
				}
			}

			return {
				...toRefs(data),
				groupArr,
				handleClick,
				handleConfirm
			}
		}
	})
</script>

<style lang="stylus">
	.followup_page {
		background: #F6F6F7;
		min-height: 100vh;

		.list {
			.cate {
				padding: 18px 0 8px 16px;
				font-size: 18px;
				font-weight: 600;
				color: #333333;
				line-height: 18px;
			}

			.item {
				display: flex;
				padding: 18px 16px 18px 16px;
				background: #FFFFFF;

				.item_left {
					flex: 1;
					font-size: 16px;
					font-weight: 400;
					color: #333333;
					line-height: 23px;
				}

				.item_right {
					width: 44rpx;
					height: 44rpx;

					image {
						width: 100%;
						height: 100%;
					}
				}
			}
		}

		.btn {
			position: fixed;
			left: 50%;
			bottom: 68rpx;
			transform: translateX(-50%);
			width: 600rpx;
			height: 96rpx;
			background: #FFD300;
			border-radius: 48rpx;
			background: #FFD300;
			border-radius: 48rpx;
			font-size: 32rpx;
			font-weight: 600;
			color: #111111;
			line-height: 45rpx;
			display: flex;
			justify-content: center;
			align-items: center;
		}
	}
</style>
